//
//  WLUserAccountViewModel.swift
//  WLWeibo
//
//  Created by Devil on 16/8/13.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit

class WLUserAccountViewModel: NSObject {
    //全局访问点
    static let sharedTools:WLUserAccountViewModel = WLUserAccountViewModel()
    //用户信息对象
    var userAccount:WLuserAccountModel?
    //路径
    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    //accessToken
    var accessToken:String?{
    
        if userAccount?.access_token != nil{
        
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            
                return userAccount?.access_token
            
            }else{
            
                return nil
            }
        
        }else{
        
            return nil
        
        }
    }
    //判断用户是否登录
    var isLogin: Bool{
    
        return accessToken != nil
    
    }
    
    override init() {
        super.init()
        //给当前类身上的属性赋值
        userAccount = getUserAccount()
    }
    
}
//保存数据
extension WLUserAccountViewModel{
    //保存对象
    func saveUserAccount(userAccount: WLuserAccountModel){
    
        self.userAccount = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: file)
    
    }
    //获得对象
    func getUserAccount() -> WLuserAccountModel?{
    
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? WLuserAccountModel
        return result
    
    }

}

extension WLUserAccountViewModel{
    //获取授权
    func getUserAccount(code: String,finish: (isSuccess: Bool)->()){
    
        WLNetworkingTools.sharedTools.oauthLoadUserAccount(code, success: { (response) in
            
            guard let res = response as? [String:AnyObject] else{
            
                finish(isSuccess: false)
                return
            }
            //字典转模型
            let userAccount = WLuserAccountModel(dict: res)
            print(userAccount)
            
            self.getUserInfo(userAccount, finish: finish)
            
            
            }) { (error) in
                
                print("请求失败:\(error)")
                finish(isSuccess: false)
        }
    
    }
    //请求个人信息
    func getUserInfo(userAccount: WLuserAccountModel,finish:(isSuccess:Bool)->()){
    
        WLNetworkingTools.sharedTools.oauthLoadUserInfo(userAccount, success: { (response) in
            
            print(response)
            
            guard let res = response as? [String:AnyObject] else{
            
                finish(isSuccess: false)
                return
            }
            //赋值
            userAccount.screen_name = res["screen_name"] as? String
            userAccount.avatar_large = res["avatar_large"] as? String
            
            print("userAccount:\(userAccount)")
            //保存对象
            self.saveUserAccount(userAccount)
            finish(isSuccess: true)
            
            }) { (error) in
                
                print("请求失败:\(error)")
                
                finish(isSuccess: false)
        }
    
    }
    
}



